package com.example.cloud.controller.business.medical;

import com.alibaba.fastjson.JSON;
import com.example.cloud.constants.StateConstants;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.business.medical.TreatPlanProduct;
import com.example.cloud.model.business.medical.TreatPlanProject;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.service.business.medical.TreatPlanProjectService;
import com.example.cloud.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by wangtao on 2018/7/2.
 */
@RestController
@RequestMapping("/treat/plan/project")
public class TreatPlanProjectController extends BaseController {

    @Autowired
    private TreatPlanProjectService treatPlanProjectService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        return this.singleQuery(model, TreatPlanProject.class);
    }

    @PostMapping("/save")
    public void save(@RequestBody TreatPlanProject record) {
        treatPlanProjectService.doSave(record);
    }

    @PostMapping("/saveOrUpdate/{patientId}")
    public void saveOrUpdate(@RequestBody List<TreatPlanProject> records, @PathVariable Long patientId) {
        treatPlanProjectService.batchInsert(records, patientId);
    }

    @PostMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        return treatPlanProjectService.doDeleteById(id);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody List<Long> ids) {
        treatPlanProjectService.doDelete(ids);
    }

    @PostMapping("/execute")
    public Result execute(@RequestBody Map map) {
        Long id = Long.valueOf(map.get("id").toString());
        int status = Integer.parseInt(map.get("status").toString());
        String content = map.get("content").toString();
        List<TreatPlanProduct> products = JSON.parseArray(map.get("products").toString(), TreatPlanProduct.class);
        TreatPlanProject project = treatPlanProjectService.findOne(id);
        if (status == StateConstants.EXECUTE_STATUS_YES) {
            // 已执行次数
            int execute = project.getExecute();
            // 总执行次数
            int quantity = project.getQuantity();
            if (execute >= quantity) {
                return ResultUtil.fail("治疗已结束");
            }
        }
        treatPlanProjectService.doExecute(id, status, content,project, products);
        return ResultUtil.success("执行成功");
    }

    @GetMapping("/findDisposal/{complaintId}")
    public Result findDisposal(@PathVariable Long complaintId) {
        return ResultUtil.success(treatPlanProjectService.doFindDisposal(complaintId));
    }

}
